package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Override
    public List<NursingLevelVo> getAll() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectList();
        // 使用转换工具类
        return convertToVoList(nursingLevels);
    }

    @Override
    public PageResponse<NursingLevelVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevel> nursingLevels = nursingLevelMapper.selectByPage(name, status);
        PageResponse<NursingLevelVo> nursingLevelVoPageResponse = PageResponse.of(nursingLevels, NursingLevelVo.class);
        return nursingLevelVoPageResponse;
    }

    @Transactional
    @Override
    public void add(NursingLevelDto nursingLevelDTO) {
        log.info(">> add事务开始");
        System.out.println(nursingLevelDTO);

        // 2. 转换DTO到Entity
        NursingLevel nursingLevel = convertDtoToEntity(nursingLevelDTO);
        System.out.println(nursingLevel);
        // 3. 设置审计字段
        LocalDateTime now = LocalDateTime.now();
        // AutoFillInterceptor 自动填充以下字段
        /*nursingLevel.setCreateTime(now);
        nursingLevel.setUpdateTime(now);
        nursingLevel.setCreateBy(1L);
        nursingLevel.setUpdateBy(1L);
        nursingLevel.setStatus(1);*/
        nursingLevelMapper.insert(nursingLevel);
        log.info("<< add事务提交");
    }

    @Transactional
    @Override
    public NursingLevelVo getById(Integer id) {
        NursingLevel nursingLevel = nursingLevelMapper.selectById(id);
        if (nursingLevel == null) {
            System.out.println("护理等级不存在");
        }
        System.out.println(nursingLevel);
        NursingLevelVo vo = convertEntityToVo(nursingLevel);
        System.out.println(vo);
        return vo;
    }

    @Transactional
    @Override
    public void update(NursingLevelDto nursingLevelDTO) {
        log.info(">> update事务开始");
        nursingLevelDTO.setPlanName(nursingPlanMapper.getPlanNameByPlanId(Long.valueOf(nursingLevelDTO.getPlanId())));
        System.out.println(nursingLevelDTO);
        NursingLevel nursingLevel = convertDtoToEntity(nursingLevelDTO);
        System.out.println(nursingLevel);
        nursingLevelMapper.update(nursingLevel);
        log.info("<< update事务提交");
    }

    @Transactional
    @Override
    public void deleteById(Long id) throws NotFoundException {
        log.info(">> delete事务开始");
        int result = nursingLevelMapper.deleteById(id);
        if (result == 0) {
            throw new NotFoundException("护理计划不存在");
        }

        log.info("<< delete事务提交");
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatusById(id, status);
    }

    private List<NursingLevelVo> convertToVoList(List<NursingLevel> entities) {
        if (entities == null) return Collections.emptyList();

        return entities.stream()
                .map(this::convertEntityToVo)
                .collect(Collectors.toList());
    }

    public NursingLevelVo convertEntityToVo(NursingLevel entity) {
        if (entity == null) {
            return null;
        }
        NursingLevelVo vo = new NursingLevelVo();
        // 1. 复制所有相同名称的字段
        BeanUtils.copyProperties(entity, vo);
        // 2. 仅处理特殊命名字段
        vo.setPlanId(entity.getLPlanId()); // lPlanId → planId
        return vo;
    }

    // DTO 转 Entity 的专用方法
    private NursingLevel convertDtoToEntity(NursingLevelDto dto) {
        NursingLevel entity = new NursingLevel();
        // 1. 复制相同名称的字段
        BeanUtils.copyProperties(dto, entity);
        // 2. 处理名称不一致的字段
        entity.setLPlanId(dto.getPlanId());  // DTO的planId → Entity的lPlanId
        return entity;
    }

}
